package com.xm.xmapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.ShortTimeOrderPayActivity;
import com.xm.xmapp.ShowURLActivity;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.Constants;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.app.RequestInterFaceCode.SeekCarCode;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.TipDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Big_customer_pay_order_infoRequest;
import com.xm.xmapp.request.bean.Get_car_statusRequest;
import com.xm.xmapp.request.bean.Handle_car_doorRequest;
import com.xm.xmapp.request.bean.Seek_carRequest;
import com.xm.xmapp.request.bean.Seek_carResponse;
import com.xm.xmapp.response.bean.BigCustomerOrderBean;
import com.xm.xmapp.response.bean.ResponseCarStatus;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.InterFaceBigCustomerOrderBeanAble;
import com.xm.xmapp.tools.Tools;

public class BigCustomerRentcarFragment extends BaseFragment implements OnClickListener {

	private TextView text3, text2, chexing, seat;
	private ImageView yuding, dianliangImg;
	private LinearLayout close, open, statusImg;
	private RelativeLayout bottom;
	private BigCustomerOrderBean bean;
	private boolean isOperateCar = true;
	private boolean isSearch = true;
	private TextView name_left;
	private LinearLayout layout;
	// private static BigCustomerRentcarFragment newFragment;
	/**
	 * 
	 * type = 1表示可以请求接口，打开车门 type = 0表示车门已开，可以关闭车门 默认是1
	 * 
	 */
	private int type = 1;

	public BigCustomerRentcarFragment() {
		super();
	}

	public static BigCustomerRentcarFragment newInstance(InterFaceBigCustomerOrderBeanAble bean) {
		// if (newFragment == null) {
		BigCustomerRentcarFragment newFragment = new BigCustomerRentcarFragment();
		newFragment.setArguments(new Bundle());
		// }
		newFragment.setData(bean);
		return newFragment;

	}

	private void setData(InterFaceBigCustomerOrderBeanAble bean) {
		this.bean = (BigCustomerOrderBean) bean;
		if (this.bean != null && view != null)
			initData();
	}

	@Override
	public int setContentView() {
		return R.layout.fragment_bigcustomer_order;
	}

	@Override
	public void initView() {

		yuding = (ImageView) findViewById(R.id.yuding);
		dianliangImg = (ImageView) findViewById(R.id.dianliangImg);
		layout = (LinearLayout) findViewById(R.id.layout);
		name_left = (TextView) findViewById(R.id.name_left);
		text3 = (TextView) findViewById(R.id.text3);
		text2 = (TextView) findViewById(R.id.text2);
		close = (LinearLayout) findViewById(R.id.close);
		open = (LinearLayout) findViewById(R.id.open);
		bottom = (RelativeLayout) findViewById(R.id.bottom);
		statusImg = (LinearLayout) findViewById(R.id.statusImg);
		chexing = (TextView) findViewById(R.id.chexing);
		seat = (TextView) findViewById(R.id.seat);

	}

	@Override
	public void ViewTreeObserver() {
		super.ViewTreeObserver();
		CustomHeightBean bean = new CustomHeightBean();
		bean.ExitOffset = layout.getHeight();
		bean.MaxOffset = bottom.getHeight();
		bean.flag = MainActivityInterFace.INTT_STATUS_ONCLICK;
		MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, bean);

	}

	@Override
	public void initData() {

		if (bean != null) {
			close.setVisibility(View.VISIBLE);
			open.setVisibility(View.VISIBLE);
			name_left.setText(bean.car_number);
			text3.setText(bean.dianLiang + "%");
			text2.setText(bean.getEndurance() + "km");
			if (bean.dianLiang < 30) {
				dianliangImg.setImageResource(R.mipmap.dianliang3);
			} else if (bean.dianLiang > 70) {
				if (bean.dianLiang != 100) {
					dianliangImg.setImageResource(R.mipmap.dianliang_icon);
				} else {
					dianliangImg.setImageResource(R.mipmap.mandian);
				}
			} else {
				dianliangImg.setImageResource(R.mipmap.dianliang2);

			}
			chexing.setText(bean.car_genre_name);
			seat.setText(bean.car_seat + "座");
		}

	}

	@Override
	public void initEvent() {
		if (bean != null) {
			yuding.setOnClickListener(this);
			close.setOnClickListener(this);
			open.setOnClickListener(this);
			statusImg.setOnClickListener(this);
			dianliangImg.setOnClickListener(this);
			text3.setOnClickListener(this);
			text2.setOnClickListener(this);
		}

	}

	private void get_car_status() {
		Get_car_statusRequest requestBean = new Get_car_statusRequest();
		requestBean.car_id = bean.car_id;
		requestBean.type = 2;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseCarStatus bean = JSON.parseObject(response, ResponseCarStatus.class);
				text3.setText(bean.getDianLiang() + "%");
				text2.setText(bean.getEndurance() + "km");

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();

			}
		});
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.text2:
		case R.id.text3:
		case R.id.dianliangImg:
			get_car_status();
			break;

		case R.id.rel:
			Intent intent = new Intent(MyAppcation.getMyAppcation(), ShortTimeOrderPayActivity.class);
			intent.putExtra("bean", bean);
			intent.putExtra("flag", true);
			startActivityForResult(intent, CodeConstant.HCActivity);
			break;
		case R.id.statusImg:
			if (isSearch) {
				isSearch = false;
				// type = 2;
				seek_carRequest();
				// operateCar();
			} else {
				Tools.showMessage("操作频繁，稍后重试");
			}

			break;
		case R.id.close:

			if (isOperateCar) {
				isOperateCar = false;
				type = 0;
				operateCar();
			} else {
				Tools.showMessage("操作频繁，稍后重试");
			}

			break;

		case R.id.open:
			if (isOperateCar) {
				isOperateCar = false;
				type = 1;
				operateCar();
			} else {
				Tools.showMessage("操作频繁，稍后重试");
			}

			break;

		case R.id.text4:
			Intent intent1 = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
			intent1.putExtra("url", Constants.Billing_content);
			intent1.putExtra("title", "计费方式");
			startActivity(intent1);
			break;

		case R.id.yuding:
			TipDialog tipDialog = new TipDialog(getActivity(), "温馨提示", "是否立即还车?", new MyDialogButton() {

				@Override
				public void setTitle(int flag, String str) {
					big_customer_pay_order_info();
				}
			});
			tipDialog.show();
			break;

		default:
			break;
		}
	}

	private void seek_carRequest() {
		Seek_carRequest requestBean = new Seek_carRequest();
		requestBean.car_id = bean.car_id;
		requestBean.type = SeekCarCode.BIGCUSTOMER_TYPE;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						isSearch = true;
					}
				}, 5000);
				Seek_carResponse bean = JSON.parseObject(response, Seek_carResponse.class);
				bean.type = 3;

				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.SEARCH_CAR_CODE,
						bean);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				isSearch = true;
				Tools.showMessage("操作失败");
			}
		});

	}

	protected void big_customer_pay_order_info() {
		Big_customer_pay_order_infoRequest requestBean = new Big_customer_pay_order_infoRequest();
		requestBean.order_id = bean.order_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				CustomHeightBean bean = new CustomHeightBean();
				bean.flag = MainActivityInterFace.STOP_USE_BIGCUSTOM_CAR;
				MyAppcation.getMyAppcation()
						.notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, bean);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();

			}
		});
	}

	/**
	 * 开关车门寻车
	 */
	private void operateCar() {

		Handle_car_doorRequest requestBean = new Handle_car_doorRequest();
		requestBean.car_id = bean.car_id;
		requestBean.type = type;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();

				try {
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							switch (type) {
							case 0:
							case 1:
								isOperateCar = true;
								break;
							case 2:
								isSearch = true;
								break;
							default:
								break;
							}

						}
					}, 5000);
					if (response.equals("true")) {

						switch (type) {
						case 0:
							Tools.showMessage("关门成功");
							break;
						case 1:
							Tools.showMessage("开门成功");
							break;
						case 2:
							Tools.showMessage("操作成功");
							break;
						default:
							break;
						}

					} else {
						Tools.showMessage("操作失败");
					}
				} catch (Exception e) {

					Tools.showMessage("操作失败");

				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				switch (type) {
				case 0:
					isOperateCar = true;
					Tools.showMessage("关门失败");
					break;
				case 1:
					isOperateCar = true;
					Tools.showMessage("开门失败");
					break;
				case 2:
					isSearch = true;
					Tools.showMessage("操作失败");
					break;
				default:
					break;
				}

				promptDialog.dismiss();
			}
		});

	}

}
